﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;
using System.Web.Http;
using ToolsModel;
using BackendLogic.BaseLogic;
using ApiLogic.BaseLogic;
using MappingDTO;
using BackstageWeb.App_Start;

namespace BackstageWeb
{
    public class Global : HttpApplication
    {
        protected  void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            //注册MVC
            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);
            //注册MVC过滤器
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            //处理URL
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            ///AutoMapper初始化
            MappingConfig.ConfigureInit();
        }


        protected void Application_Error(object sender, EventArgs e) {
            Exception ex = Server.GetLastError();
            if (ex is HttpException)
            {
                switch (((HttpException)(ex)).GetHttpCode())
                {
                    case 404:
                        Server.Transfer("/block404.html", false);
                        break;
                    case 500:
                        Server.Transfer("/block500.html", false);
                        break;
                    default:
                        break;
                }
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            UserSession.AddLoginUser(new AdminTicket());
            ClientSession.AddClientInfo(new ClientTicket());
        }

        /// <summary>
        /// WebApiSession启动
        /// </summary>
        public override void Init()
        {
            this.PostAuthenticateRequest += (sender, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
            base.Init();
        }
    }
}